package com.yx.cloud.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.widget.NestedScrollView;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.iot.common.utils.Event;
import com.iot.common.utils.LogUtils;
import com.iot.product.sweeper.bean.CleanSettings;
import com.iot.product.sweeper.bean.Robot;
import com.iot.product.sweeper.bean.RobotStatus;
import com.iot.sweeper.AppContext;
import com.iot.sweeper.param.CleanSettingParam;
import com.yx.cloud.R;
import com.yx.cloud.product.ProductConfigConstants;
import com.yx.cloud.utils.DisplayUtil;
import com.yx.cloud.utils.RomUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

public class BottomBaseStationDialogLayout extends BottomSheetDialogFragment implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
    private Context mContext;
    private View view;
    private CheckBox dust_switch;
    private Robot robot;
    private CleanSettings cleanSettings;
    private LinearLayout llBaseWorkModeLayout, llBaseWorkStatusLayout, llAutomaticCleanTime, llBackwashFrequency;
    private ImageView ivBaseShimizuBoxState, ivBaseSewageBoxState, ivBaseDustBagState;
    private TextView tvBaseShimizuBoxState, tvBaseSewageBoxState, tvBaseDustBagState;
    private RadioGroup rgCleanMopBackwashFrequency, rgCleanTimes, rgDryingTime, rgAutomaticCleanTimes;
    private RadioButton rbBackwashFrequency10, rbBackwashFrequency20, rbBackwashFrequency30,
            rgCleanTimesOne, rgCleanTimesTwo, rgCleanTimesThree, rgDryingTimeTwo, rgDryingTimeThree, rgDryingTimeFour,
            rbDustImmediately, rbInstantDrying, rbCleaningMop, rbReturnCharge, rbReturnSelfCleaning,
            rbAutomaticCleanTimesOne, rbAutomaticCleanTimesTwo, rbAutomaticCleanTimesThree;
    private BottomSheetBehavior<FrameLayout> behavior;
    private JSONObject config;

    public static BottomBaseStationDialogLayout getInstance() {
        return new BottomBaseStationDialogLayout();
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        return new BottomSheetDialog(this.getContext());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContext = getContext();
        Log.e("TAG", "onCreateView: ");
        view = inflater.inflate(R.layout.bottombasestation_dialog_layout, container, false);
        initViews(view);
        initData();
        return view;
    }

    @Override
    public void onStart() {
        Log.e("TAG", "onStart: ");
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }
        //获取dialog对象
        BottomSheetDialog dialog = (BottomSheetDialog) getDialog();
        //把windowsd的默认背景颜色去掉，不然圆角显示不见
        dialog.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //获取diglog的根部局
        FrameLayout bottomSheet = dialog.getDelegate().findViewById(R.id.design_bottom_sheet);
        if (bottomSheet != null) {
            //获取根部局的LayoutParams对象
            CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomSheet.getLayoutParams();
            layoutParams.height = getPeekHeight();
            //修改弹窗的最大高度，不允许上滑（默认可以上滑）
            bottomSheet.setLayoutParams(layoutParams);

            behavior = BottomSheetBehavior.from(bottomSheet);
            //peekHeight即弹窗的最大高度
            behavior.setPeekHeight(getPeekHeight());

            NestedScrollView scroll_view = bottomSheet.findViewById(R.id.scrollView);

            int height = (int) (getPeekHeight() * 0.8);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, height);
            params.topMargin = 40;
            scroll_view.setLayoutParams(params);
            // 初始为展开状态
            behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            View mReBack = view.findViewById(R.id.re_back);
            //设置监听
            mReBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //关闭弹窗
                    behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
                }
            });
        }

    }

    private void initViews(View view) {
        ivBaseShimizuBoxState = view.findViewById(R.id.iv_base_shimizuBoxState);//清水箱
        tvBaseShimizuBoxState = view.findViewById(R.id.tv_base_shimizuBoxState);//清水箱

        ivBaseSewageBoxState = view.findViewById(R.id.iv_base_sewageBoxState);//污水箱
        tvBaseSewageBoxState = view.findViewById(R.id.tv_base_sewageBoxState);//污水箱

        ivBaseDustBagState = view.findViewById(R.id.iv_base_dustBagState);//集尘袋
        tvBaseDustBagState = view.findViewById(R.id.tv_base_dustBagState);//集尘袋

        dust_switch = view.findViewById(R.id.dust_switch);//自动集尘开关
        rgCleanMopBackwashFrequency = view.findViewById(R.id.rg_cleanMop_BackwashFrequency);//回洗频率
        rbBackwashFrequency10 = view.findViewById(R.id.rb_BackwashFrequency10);//回洗频率
        rbBackwashFrequency20 = view.findViewById(R.id.rb_BackwashFrequency20);//回洗频率
        rbBackwashFrequency30 = view.findViewById(R.id.rb_BackwashFrequency30);//回洗频率
        llAutomaticCleanTime = view.findViewById(R.id.ll_automaticCleanTime);//自动集尘频率
        llBackwashFrequency = view.findViewById(R.id.ll_backwashFrequency);//自动集尘频率
        rgCleanTimes = view.findViewById(R.id.rg_cleanTimes);//清洗次数
        rgCleanTimesOne = view.findViewById(R.id.rg_cleanTimesOne);//清洗次数
        rgCleanTimesTwo = view.findViewById(R.id.rg_cleanTimesTwo);//清洗次数
        rgCleanTimesThree = view.findViewById(R.id.rg_cleanTimesThree);//清洗次数
        rgDryingTime = view.findViewById(R.id.rg_dryingTime);//烘干时间
        rgDryingTimeTwo = view.findViewById(R.id.rg_dryingTimeTwo);//烘干时间
        rgDryingTimeThree = view.findViewById(R.id.rg_dryingTimeThree);//烘干时间
        rgDryingTimeFour = view.findViewById(R.id.rg_dryingTimeFour);//烘干时间

        llBaseWorkModeLayout = view.findViewById(R.id.ll_baseWorkModeLayout);//返回充电
        rbReturnCharge = view.findViewById(R.id.rb_returnCharge);//返回充电
        rbReturnSelfCleaning = view.findViewById(R.id.rb_ReturnSelfCleaning);//返回自清洁

        llBaseWorkStatusLayout = view.findViewById(R.id.ll_baseWorkStatusLayout);//立即集尘
        rbDustImmediately = view.findViewById(R.id.rb_DustImmediately);//立即集尘
        rbInstantDrying = view.findViewById(R.id.rb_InstantDrying);//立即烘干
        rbCleaningMop = view.findViewById(R.id.rb_CleaningMop);//清洗拖布

        rgAutomaticCleanTimes = view.findViewById(R.id.rg_automaticCleanTimes);//自动集尘频率
        rbAutomaticCleanTimesOne = view.findViewById(R.id.rb_automaticCleanTimesOne);//一次后
        rbAutomaticCleanTimesTwo = view.findViewById(R.id.rb_automaticCleanTimesTwo);//2次后
        rbAutomaticCleanTimesThree = view.findViewById(R.id.rb_automaticCleanTimesThree);//3次后

    }

    private void initData() {
        robot = AppContext.get().getCurrentDevice();
        config = AppContext.getConfig();
        DisplayUtil.show(llAutomaticCleanTime, config.optBoolean(ProductConfigConstants.automaticCleanTime));//自动集尘频率
        // DisplayUtil.show(llBackwashFrequency, !config.optBoolean(ProductConfigConstants.automaticCleanTime));//回洗频率、清洗次数
        // DisplayUtil.show(rbCleaningMop, !config.optBoolean(ProductConfigConstants.automaticCleanTime));

//        if (config.optBoolean(ProductConfigConstants.automaticCleanTime)) {
//            rbCleaningMop.setText("清洗滚筒");
//        } else {
//            rbCleaningMop.setText("清洗拖布");
//        }
        rgCleanMopBackwashFrequency.setOnCheckedChangeListener(this);
        rgCleanTimes.setOnCheckedChangeListener(this);
        rgDryingTime.setOnCheckedChangeListener(this);
        rbDustImmediately.setOnClickListener(this);
        rbInstantDrying.setOnClickListener(this);
        rbCleaningMop.setOnClickListener(this);
        rbReturnCharge.setOnClickListener(this);
        rbReturnSelfCleaning.setOnClickListener(this);
        rgAutomaticCleanTimes.setOnCheckedChangeListener(this);

        cleanSettings = robot.getCleanSettings();
        waterTankDustBagState(cleanSettings.getBaseStationStatus());//水箱状态/集尘袋状态

        if ("0".equals(cleanSettings.getAutoDust())) {
            dust_switch.setChecked(false);
        } else {
            dust_switch.setChecked(true);
        }
        setBackWaskFrequency(cleanSettings.getBackwashFrequency());//回洗频率
        setCleanMopTimes(cleanSettings.getCleanMopFrequency());//清洗拖布次数
        dryingTime(cleanSettings.getDryingTime());//烘干时间
        setAutomaticDustCollectionFrequency(cleanSettings.getAutoDust());//自动集尘频率次数

        workStatus();
        returnChargeState();
        selfCleaningState();
        //自动集尘开关 ：0关 1开
        dust_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @SuppressLint("LongLogTag")
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!buttonView.isPressed()) {
                    return;
                }
                LogUtils.i(" isChecked ---------- " + isChecked);
                if (isChecked) {
                    AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().autoDustFrequency(1).build());
                } else {
                    AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().autoDustFrequency(0).build());
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        switch (v.getId()) {
            case R.id.rb_returnCharge:
                //返回充电
                baseStationWorkStatus("charging");
                behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
                break;
            case R.id.rb_ReturnSelfCleaning:
                //返回自清洁
                baseStationWorkStatus("selfCleaning");
                behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
                break;
            case R.id.rb_DustImmediately:
                //立即集尘
                AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().autoDust(true).build());
                //baseStationWorkMode("dust_collection");
                behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
                break;
            case R.id.rb_InstantDrying:
                //立即烘干
                AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().dryState(true).build());
                //baseStationWorkMode("drying");
                behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
                break;
            case R.id.rb_CleaningMop:
                //ZW2X U3PRO清洗拖布 ZY2X清洗滚筒
                AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().cleanMop(true).build());
                behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        switch (event.getType()) {
            case Event.MSG_ROBOT_SETTINGS:
                //回洗频率
                int frequency = cleanSettings.getBackwashFrequency(); //回洗频率
                setBackWaskFrequency(frequency);
                break;
            case Event.MESSAGE_TYPE_DRYING_TIME:
                //烘干时间
                String dryingTime = cleanSettings.getDryingTime();
                dryingTime(dryingTime);
                break;
            case Event.MESSAGE_TYPE_CLEAN_MOP_FREQUENCY:
                //清洗拖布次数
                int cleanMopFrequency = cleanSettings.getCleanMopFrequency();//清洗拖布次数
                setCleanMopTimes(cleanMopFrequency);
                break;
            case Event.MSG_ROBOT_STATE:
                returnChargeState();
                //返回自清洁
                selfCleaningState();
                break;
            case Event.MESSAGE_TYPE_BASE_WATERTANK_STATUS:
                //基站水箱状态
                waterTankDustBagState(robot.getCleanSettings().getBaseStationStatus());
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        int id = radioGroup.getId();
        if (id == R.id.rg_automaticCleanTimes) {
            //自动集尘频率
            LogUtils.i("sendToDevice         setAutomaticDustCollectionFrequency1111");
            setAutomaticDustCollectionFrequency(radioGroup, checkedId);
        } else if (id == R.id.rg_cleanMop_BackwashFrequency) {
            //回洗频率
            setBackwashFrequency(radioGroup, checkedId);
        } else if (id == R.id.rg_cleanTimes) {
            //清洗次数
            setCleanTimes(radioGroup, checkedId);
        } else if (id == R.id.rg_dryingTime) {
            //烘干时间
            setDryingTime(radioGroup, checkedId);
        }
    }

    private void waterTankDustBagState(String baseStationStatus) {
//        00(清水箱正常)00(污水箱正常)00(集尘袋正常)-----绿色 shimizuBox, sewageBox, dustBag
//        01(清水箱缺水)01(污水箱已满 )01(集尘袋已满)-----黄色
//        02(清水箱未安装)02(污水箱未安装 )02(集尘袋未安装)-----黄色
        if (baseStationStatus != null && !baseStationStatus.equals("") && baseStationStatus.length() == 6) {
            String shimizuBox = baseStationStatus.substring(0, 2);
            String sewageBox = baseStationStatus.substring(2, 4);
            String dustBag = baseStationStatus.substring(4, 6);
            LogUtils.i(">>>> shimizuBox: " + shimizuBox + " sewageBox: " + sewageBox + " dustBag: " + dustBag);
            if ("00".equals(shimizuBox)) {
                tvBaseShimizuBoxState.setText(getString(R.string.BS_State_FreshWaterTank_Normal));
                ivBaseShimizuBoxState.setImageResource(R.mipmap.green_icon);
            } else if ("01".equals(shimizuBox)) {
                ivBaseShimizuBoxState.setImageResource(R.mipmap.yellow_icon);
                tvBaseShimizuBoxState.setText(getString(R.string.BS_State_FreshWaterTank_Abnormal));
            } else if ("02".equals(shimizuBox)) {
                ivBaseShimizuBoxState.setImageResource(R.mipmap.yellow_icon);
                tvBaseShimizuBoxState.setText(getString(R.string.BS_State_FreshWaterTank_Absent));
            }
            if ("00".equals(sewageBox)) {
                ivBaseSewageBoxState.setImageResource(R.mipmap.green_icon);
                tvBaseSewageBoxState.setText(getString(R.string.BS_State_SewageTank_Normal));
            } else if ("01".equals(sewageBox)) {
                ivBaseSewageBoxState.setImageResource(R.mipmap.yellow_icon);
                tvBaseSewageBoxState.setText(getString(R.string.BS_State_SewageTank_Abnormal));
            } else if ("02".equals(sewageBox)) {
                ivBaseSewageBoxState.setImageResource(R.mipmap.yellow_icon);
                tvBaseSewageBoxState.setText(getString(R.string.BS_State_SewageTank_Absent));
            }
            if ("00".equals(dustBag)) {
                ivBaseDustBagState.setImageResource(R.mipmap.green_icon);
                tvBaseDustBagState.setText(getString(R.string.BS_State_DustBag_Normal));
            } else if ("01".equals(dustBag)) {
                ivBaseDustBagState.setImageResource(R.mipmap.yellow_icon);
                tvBaseDustBagState.setText(getString(R.string.BS_State_DustBag_Abnormal));
            } else if ("02".equals(dustBag)) {
                ivBaseDustBagState.setImageResource(R.mipmap.yellow_icon);
                tvBaseDustBagState.setText(getString(R.string.BS_State_DustBag_Absent));
            }
        }
    }


    /**
     * 设置初始化设置自动集尘频率
     */
    public void setAutomaticDustCollectionFrequency(String times) {
        if ("1".equals(times)) {
            rbAutomaticCleanTimesOne.setChecked(true);
        } else if ("2".equals(times)) {
            rbAutomaticCleanTimesTwo.setChecked(true);
        } else if ("3".equals(times)) {
            rbAutomaticCleanTimesThree.setChecked(true);
        }
    }

    /**
     * 设置自动集尘频率
     */
    private void setAutomaticDustCollectionFrequency(RadioGroup group, int checkedId) {
        LogUtils.i("sendToDevice         setAutomaticDustCollectionFrequency 222");
        View viewById = group.findViewById(checkedId);
        if (!viewById.isPressed()) {
            return;
        }
        int times = 0;
        if (checkedId == R.id.rb_automaticCleanTimesOne) {
            times = 1;
        } else if (checkedId == R.id.rb_automaticCleanTimesTwo) {
            times = 2;
        } else if (checkedId == R.id.rb_automaticCleanTimesThree) {
            times = 3;
        }
        AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().autoDustFrequency(times).build());
    }

    /**
     * 设置初始化设置回洗频率
     */
    public void setBackWaskFrequency(int frequency) {
        if (frequency == 10) {
            rbBackwashFrequency10.setChecked(true);
        } else if (frequency == 20) {
            rbBackwashFrequency20.setChecked(true);
        } else if (frequency == 30) {
            rbBackwashFrequency30.setChecked(true);
        }
    }

    /**
     * 设置回洗频率
     */
    private void setBackwashFrequency(RadioGroup group, int checkedId) {
        View viewById = group.findViewById(checkedId);
        if (!viewById.isPressed()) {
            return;
        }
        int frequency = 0;
        if (checkedId == R.id.rb_BackwashFrequency10) {
            frequency = 10;
        } else if (checkedId == R.id.rb_BackwashFrequency20) {
            frequency = 20;
        } else if (checkedId == R.id.rb_BackwashFrequency30) {
            frequency = 30;
        }
        AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().backwashFrequency(frequency).build());
    }

    /**
     * 设置初始化清洗拖布次数
     */
    public void setCleanMopTimes(int times) {
        if (times == 1) {
            rgCleanTimesOne.setChecked(true);
        } else if (times == 2) {
            rgCleanTimesTwo.setChecked(true);
        } else if (times == 3) {
            rgCleanTimesThree.setChecked(true);
        }
    }

    /**
     * 清洗拖布次数
     *
     * @param group
     * @param checkedId
     */
    private void setCleanTimes(RadioGroup group, int checkedId) {
        View viewById = group.findViewById(checkedId);
        if (!viewById.isPressed()) {
            return;
        }
        int cleanTimes = 0;
        if (checkedId == R.id.rg_cleanTimesOne) {
            cleanTimes = 1;
        } else if (checkedId == R.id.rg_cleanTimesTwo) {
            cleanTimes = 2;
        } else if (checkedId == R.id.rg_cleanTimesThree) {
            cleanTimes = 3;
        }
        AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().setCleanMopFrequency(cleanTimes).build());
    }

    /**
     * 设置初始化烘干时间
     */
    public void dryingTime(String times) {
        if (times != null) {
            if (times.equals("low")) {
                rgDryingTimeTwo.setChecked(true);
            } else if (times.equals("middle")) {
                rgDryingTimeThree.setChecked(true);
            } else if (times.equals("high")) {
                rgDryingTimeFour.setChecked(true);
            }
        }
    }

    /**
     * 烘干时间
     *
     * @param group
     * @param checkedId
     */
    private void setDryingTime(RadioGroup group, int checkedId) {
        View viewById = group.findViewById(checkedId);
        if (!viewById.isPressed()) {
            return;
        }
        String dryingTime = "";
        if (checkedId == R.id.rg_dryingTimeTwo) {
            dryingTime = "low";
        } else if (checkedId == R.id.rg_dryingTimeThree) {
            dryingTime = "middle";
        } else if (checkedId == R.id.rg_dryingTimeFour) {
            dryingTime = "high";
        }
        AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().setDryingTime(dryingTime).build());
    }

    /**
     * 基站工作状态
     */
    private void baseStationWorkStatus(String type) {
        if ("charging".equals(type)) {
            //返回充电
            String text = rbReturnCharge.getText().toString();
            if (text.equals(getString(R.string.BS_Recharge_Start))) {
                //发送开始回充
                //rbReturnCharge.setText(getString(R.string.pause_charge));
                AppContext.getProductService().startRecharge();
                // rbReturnCharge.setChecked(false);
            } else {
                //当前是继续回充，应发送暂停回充
                //rbReturnCharge.setText(getString(R.string.return_charge));
                AppContext.getProductService().pauseRecharge();
                // rbReturnCharge.setChecked(true);
            }
        } else if ("selfCleaning".equals(type)) {
            //返回自清洁
            int workState = robot.getRobotStatus().getWorkState();
            LogUtils.i("返回自清洁 1111111111111: " + workState);
            if (workState == RobotStatus.STATE_GOTO_BS) {
//                rbReturnSelfCleaning.setText(getString(R.string.cancel_return));
//                rbReturnSelfCleaning.setChecked(false);
                AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().setSelfCleaning(false).build());
            } else {
//                rbReturnSelfCleaning.setText(getString(R.string.return_selfCleaning));
//                rbReturnSelfCleaning.setChecked(true);
                AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().setSelfCleaning(true).build());
            }
        }
    }

    private void workStatus() {
        int workState = robot.getRobotStatus().getWorkState();
        if (workState == RobotStatus.STATE_CHARGING || workState == RobotStatus.STATE_WATER_INJECTION
                || workState == RobotStatus.STATE_CLEANING_MOP || workState == RobotStatus.STATE_DRYING_MOP
                || workState == RobotStatus.STATE_DUST_COLLECTION) {
            //显示3个
            llBaseWorkModeLayout.setVisibility(View.GONE);
            llBaseWorkStatusLayout.setVisibility(View.VISIBLE);
        } else {
            llBaseWorkModeLayout.setVisibility(View.VISIBLE);
            llBaseWorkStatusLayout.setVisibility(View.GONE);
        }
    }

    /**
     * 基站工作模式
     */
    private void selfCleaningState() {
        //返回自清洁 true：：返回自清洁； false：取消返回
        int workState = robot.getRobotStatus().getWorkState();
        LogUtils.i("返回自清洁 上报 : " + workState);
        if (workState == RobotStatus.STATE_GOTO_BS) {
            rbReturnSelfCleaning.setText(getString(R.string.BS_SelfClean_End));
            rbReturnSelfCleaning.setChecked(false);
        } else {
            rbReturnSelfCleaning.setText(getString(R.string.BS_SelfClean_Start));
            rbReturnSelfCleaning.setChecked(true);
        }
    }

    private void returnChargeState() {
        LogUtils.i("回充按钮 returnChargeState ");
        int state = robot.getRobotStatus().getWorkState();
        int mode = robot.getRobotStatus().getWorkMode();
        if (state == RobotStatus.STATE_WORKING && mode == RobotStatus.MODE_RECHARGE) {
            //回充中 显示暂停
            rbReturnCharge.setText(getString(R.string.BS_Recharge_Pause));
            rbReturnCharge.setChecked(false);
        } else {
            //其他 返回充电
            rbReturnCharge.setText(getString(R.string.BS_Recharge_Start));
            rbReturnCharge.setChecked(true);
        }
    }

    /**
     * 弹窗高度，默认为屏幕高度的四分之三
     * 子类可重写该方法返回peekHeight
     *
     * @return height
     */
    protected int getPeekHeight() {
        int peekHeight = getResources().getDisplayMetrics().heightPixels;
        //设置弹窗高度为屏幕高度的3/4
        return peekHeight - peekHeight / 10;
    }

    @Override
    public void onPause() {
        super.onPause();
        if (EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().unregister(this);
        }
    }

}
